package com.example.zzl.model.HomeFragment;

import android.util.Log;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.example.zzl.model.bean.Home;
import com.example.zzl.model.cans.HomeUrl;
import com.example.zzl.utils.specification.VolleySingleton;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;


/**
 * Created by ligg on 2017/2/10.
 */

public class NewsModel implements Response.Listener<String> {

    private ICallbackM callback;
    private List<Home.News> list;

    public interface ICallbackM {
        void loadFinished();
    }

    public NewsModel(ICallbackM callback) {
        this.callback = callback;
    }

    public void loadData() {
        StringRequest stringRequest = new StringRequest(HomeUrl.News, this, VolleySingleton.volleySingleton);
        Log.e("NewsModel", "loadData: ");
        VolleySingleton.volleySingleton.getRequestQueue().add(stringRequest);

    }

    @Override
    public void onResponse(String response) {
        Type type = new TypeToken<List<Home.News>>() {
        }.getType();
        Log.e("NewsModel","数据");
        list = new Gson().fromJson(response, type);
        callback.loadFinished();
    }

    public List<Home.News> getList() {
        return list;
    }
}
